//
//  FavoriteViewController.m
//  SZBus3
//
//  Created by Kengi Chan on 10/22/09.
//  Copyright 2009 IDMT. All rights reserved.
//

#import "SZBusAppDelegate.h"
#import "StationViewController.h"
#import "Station.h"


@implementation StationViewController

@synthesize table;
@synthesize search;
@synthesize filterStationArray;

#pragma mark -
#pragma mark Search Customize methods
- (void)resetSearch{
	[self.filterStationArray removeAllObjects];
}

- (void)handleSearchForTerm:(NSString *)searchTerm{

	self.filterStationArray = [Station getDataForSearch:searchTerm];	
	[table reloadData];
}
/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/
#pragma mark -
#pragma mark UIViewController methods

- (void)viewDidLoad {

	[super viewDidLoad];
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    //self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	[self.table reloadData];
}
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
	
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	self.table = nil;
    self.search = nil;
	self.filterStationArray =nil;
	// Release any retained subviews of the main view.
}


- (void)dealloc {
	[table release];
	[search release];
	[filterStationArray release];
	[super dealloc];
}


#pragma mark Table view methods
- (CGFloat)tableView:(UITableView *)tableView 
	heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 36.0f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView 
	numberOfRowsInSection:(NSInteger)section 
{
	
	NSInteger stationCnt = [filterStationArray count];
	//NSLog(@"103 %@" ,routeCnt);
    return stationCnt;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
	cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{   
	
	static NSString *StationCellIdentifier = @"stationCell";
    
    UITableViewCell *stationCell = [tableView dequeueReusableCellWithIdentifier:StationCellIdentifier];
    if (stationCell == nil) {
#ifdef __IPHONE_3_0
		stationCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier: StationCellIdentifier] autorelease];
#else
        stationCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
									   reuseIdentifier:StationCellIdentifier] autorelease];
#endif
    }
	Station *station = [filterStationArray objectAtIndex:indexPath.row];
	UIImage *image = [UIImage imageNamed:@"imgstation.png"];	
	
#ifdef __IPHONE_3_0
	stationCell.imageView.image = image;
    stationCell.textLabel.text = station.stationName;	
	stationCell.textLabel.font = [UIFont systemFontOfSize:18.0 ];
	//cell.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
#else	
	stationCell.image = image;
    stationCell.text = station.stationName;	
	stationCell.font = [UIFont boldSystemFontOfSize:18.0];
#endif
	
    return stationCell;
}

#pragma mark -
#pragma mark Table View Delegate Methods

- (NSIndexPath *)tableView:(UITableView *)tableView
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	[search resignFirstResponder];
//	NSUInteger row = [indexPath row];
//    if (row == 0)
//        return nil;
	return indexPath;
}

- (void)tableView:(UITableView *)tableView 
	didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	//NSUInteger section = [indexPath section];
//	NSUInteger row = [indexPath row];
	
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//	return listFavoriteType;
//}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark -
#pragma mark Search Bar Delegate Methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{	
	NSString *searchTerm = [searchBar text];
	NSLog(@"SearchButtonClicked %@",searchTerm);
	[self handleSearchForTerm:searchTerm];
}

- (void)searchBar:(UISearchBar *)searchBar	
	textDidChange:(NSString *)searchTerm{	
	NSLog(@"textDidChange %@",searchTerm);
	if([searchTerm length] == 0){
		[self resetSearch];
		[table reloadData];
		return;
	}
	[self handleSearchForTerm:searchTerm];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
	NSLog(@"CancalButtonClicked");	
	searchBar.text = @"";	
	[self resetSearch];
	[table reloadData];
	[searchBar resignFirstResponder];
}


@end

